import { ref } from 'vue'
import { defineStore } from 'pinia'
import type { UserInfoVo, TbUser, TbUserInfo, TbUserStatisticsVo } from '@/types/UserInfo'
import type { BaseDataResult } from '@/types/BaseResult'
import { Api } from '@/types/Api'

export const useUserStore = defineStore('userStore', () => {
  const user = ref({} as TbUser)
  const userInfo = ref({} as TbUserInfo)
  const userStatistics = ref({} as TbUserStatisticsVo)
  const userlogo = ref('')
  const islogin = ref(false)

  const updateUserInfo = (cb: (data: boolean) => void = () => {}) => {
    Api.get('/auth/info', {}, (data: BaseDataResult<UserInfoVo>) => {
      islogin.value = data.success
      if (data.success) {
        user.value = data.data.user
        userInfo.value = data.data.info
        userStatistics.value = data.data.statistics
        userlogo.value = Api.userlogo(user.value.id)
      } else {
        user.value = {} as TbUser
        userInfo.value = {} as TbUserInfo
        userStatistics.value = {} as TbUserStatisticsVo
        userlogo.value = ''
      }
      cb(data.success)
    })
  }

  return {
    user,
    userInfo,
    userStatistics,
    userlogo,
    islogin,
    updateUserInfo,
  }
})
